package com.itzyf.pokemondata.utils

import com.avos.avoscloud.AVException
import com.avos.avoscloud.AVException.CONNECTION_FAILED
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout
import com.tydic.baselibrary.activity.BaseActivity
import java.net.ConnectException

/**
 * @author 依风听雨
 * @version 创建时间：2018/12/24 15:48
 */
fun QMUIPullRefreshLayout.setOnRefreshListener(listener: OnRefreshListener) {
    setOnPullListener(object : QMUIPullRefreshLayout.OnPullListener {
        override fun onMoveTarget(offset: Int) {

        }

        override fun onMoveRefreshView(offset: Int) {

        }

        override fun onRefresh() {
            listener.onRefresh()
        }
    })
}

interface OnRefreshListener {
    fun onRefresh()
}

fun BaseActivity.showErrToast(e: AVException) {
    if (e.code == 0) {
        showToast("网络异常，请检查网络")
    } else {
        showToast("服务器异常")
    }
}
/**
 * 使用java正则表达式去掉多余的.与0
 * @return
 */
fun Double.subZeroAndDot(): String {
    var s = this.toString()
    if (s.indexOf(".") > 0) {
        s = s.replace("0+?$".toRegex(), "")//去掉多余的0
        s = s.replace("[.]$".toRegex(), "")//如最后一位是.则去掉
    }
    return s
}